package com.googlecode.FWT.view.pageloader
{
	import com.googlecode.FWT.core.NotificationNames;
	import com.googlecode.FWT.model.ProjectDataProxy;
	import com.googlecode.FWT.view.pages.activity.FreeWrite;
	import com.googlecode.FWT.view.pages.activity.FreeWriteMediator;
	import com.googlecode.FWT.view.pages.introduction.IntroductionPage;
	import com.googlecode.FWT.view.pages.introduction.IntroductionPageMediator;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * Integrates the PageLoader view component with the PureMVC framework. 
	 * @author ~martin
	 * 
	 */
	public class PageLoaderMediator extends Mediator
	{
		
	/**
		 * PureMVC identifier 
		 */
	public static const NAME:String = "PageLoaderMediator";
	
		/**
		 * Constructor. 
		 * @author ~martin
		 * 
		 */
		public function PageLoaderMediator(viewComponent:PageLoader)
		{
			super(NAME, viewComponent);
		}
		
		/**
		 * Called once the mediator has been registered. Used to load first page. 
		 * 
		 */
		override public function onRegister():void
		{
			// find the number of loaded pages
			var numPages:int = viewRef.numPages;
			sendNotification(NotificationNames.NUMBER_PAGES, numPages);
			
			// load the first page
			viewRef.loadPage(0);
			
			// load its associated mediator
			//facade.registerMediator(new IntroductionPageMediator(viewRef.loadedPage as IntroductionPage));
			
		}
		/**
		 * Notify interest in page change events. 
		 * @return 
		 * 
		 */
		override public function listNotificationInterests():Array
		{
			return [ProjectDataProxy.PAGE_INDEX_CHANGED]
		}
		
		/**
		 * Respond to page change events.
		 * @param notification
		 * 
		 */
		override public function handleNotification(notification:INotification):void
		{
			super.handleNotification(notification);
			
			var index:int = notification.getBody() as int;
			
			switch(notification.getName())
			{
				case ProjectDataProxy.PAGE_INDEX_CHANGED:
					// load page
					loadPage(index);
					break;
			}
		}
		
		/**
		 * 
		 * @param index
		 * 
		 */
		protected function loadPage(index:int):void
		{
			// get a reference to ProjectDataProxy
			var proxy:ProjectDataProxy = facade.retrieveProxy(ProjectDataProxy.NAME) as ProjectDataProxy;
			
			// get the current loaded page
			var loadedPageIndex:int = proxy.loadedPageIndex;
			
			// remove the existing page mediator, if present
			if(viewRef.loadedPage as IntroductionPage)
			{
				facade.removeMediator(IntroductionPageMediator.NAME);
			}
			
			if(viewRef.loadedPage as FreeWrite)
			{
				facade.removeMediator(FreeWriteMediator.NAME);
			}
			
			// load the appropriate page
			viewRef.loadPage(index);
			
			// check the page has been added
			if(viewRef.loadedPage == null)
			{
				throw(new Error("The PageLoader instance has not loaded the page correctly."));
			}
			
			// register the appropriate mediators
			if(viewRef.loadedPage as IntroductionPage)
			{
				facade.registerMediator(new IntroductionPageMediator(viewRef.loadedPage as IntroductionPage));
			}
			
			if(viewRef.loadedPage as FreeWrite)
			{
				facade.registerMediator(new FreeWriteMediator(viewRef.loadedPage as FreeWrite));
			}
		}
		/**
		 * Return a typed reference to the viewComponent.  
		 * @return
		 */		
		protected function get viewRef():PageLoader
		{
			return viewComponent as PageLoader;
		}
	}
}